package com.ruijie.acl.web;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Transactional;

import com.ruijie.acl.Template;
import com.ruijie.acl.Variable;
import com.ruijie.acl.service.VariableService;
import com.ruijie.foundation.bo.EntityObject;
import com.ruijie.foundation.dao.support.Page;
import com.ruijie.foundation.exception.BusinessException;
import com.ruijie.foundation.service.Repository;
import com.ruijie.foundation.web.intercept.AddSuccessInfo;

@Name("variableAction")
@Scope(ScopeType.CONVERSATION)
@AddSuccessInfo
public class VariableAction extends AclBaseAction {

	@In("#{variableService}")
	private VariableService variableService;

	private Variable model = new Variable();

	private Variable criteria = new Variable();

	@Override
	@SuppressWarnings("unchecked")
	protected Repository getRepository() {
		return this.variableService;
	}

	@Override
	protected String getAddPage() {
		return "/acl/aclTemplate/variableAdd.seam";
	}

	@Override
	public EntityObject getCriteria() {
		return this.criteria;
	}

	@Override
	protected String getDetailPage() {
		//return "/acl/variableDetail.seam";
		return null;
	}

	@Override
	protected String getListPage() {
		return "/acl/aclTemplate/templateDetail.seam";
	}

	@Override
	public EntityObject getModel() {
		return this.model;
	}

	@Override
	protected String getUpdatePage() {
		return "/acl/aclTemplate/variableEdit.seam";
	}

	@Override
	public void resetModel() {
		model = null;
		model = new Variable();
	}

	@Override
	public void setCriteria(EntityObject criteria) {
		this.criteria = (Variable) criteria;
	}

	@Override
	public void setModel(EntityObject model) {
		this.model = (Variable) model;
	}

	@Override
    @SuppressWarnings("unchecked")
    @Transactional
    protected boolean doAdd() {
		// 如果存在重名
		if (this.variableService.isExistVariableName(this.model.getName(), 
				(Template)this.getParentEntity(), null)){
			throw new BusinessException("acl_validator_template_var_name_repeat");
		}
		this.model.setTemplate((Template)this.getParentEntity());
		this.variableService.save(this.model);
		return true;
    }
	
	@Override
	@SuppressWarnings("unchecked")
	@Transactional
  	protected boolean doUpdate() {
		// 如果存在重名
		if (this.variableService.isExistVariableName(this.model.getName(), 
				(Template)this.getParentEntity(), this.model.getId())){
			throw new BusinessException("acl_validator_template_var_name_repeat");
		}
		this.model.setTemplate((Template)this.getParentEntity());
		this.variableService.update(this.model);
		return true;
	}	
	
	/**
	 * 不分页地获取变量列表
	 * @param template 变量所属模板

	 * @return
	 */
	public List<Variable> getEntityListNoPage(Template template){
		this.model.setTemplate(template);
		List<Variable> entitys=this.variableService.getByTemplate(template);
		Set<Variable> vars=new HashSet<Variable>();
		vars.addAll(entitys);
		template.setVariables(vars);
		return entitys;
	}
	protected Page pagedQuery(EntityObject criteria, int page, int pageSize) {
		return variableService.pageQueryByTemplate((Template)this.getParentEntity(), page, pageSize);
	}	
	
	//-------------modify by sinyee 2010-10-22 being----------------

	public String toAddPageBlank(){
		this.toAddPage();
		return null;
	}
	
	@AddSuccessInfo
	public String addEntity(){
		this.add();
		return null;
	}

	public String toUpdatePageBlank(EntityObject model){
		this.toUpdatePage(model);
		return null;
	}
	
	@AddSuccessInfo
	public String updateEntity(){
		this.update();
		return null;
	}
	
	//--------------modify by sinyee 2010-10-22 end----------------
	
}
